package com.jv.viewpagerlearning;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ViewPager viewpager;
    private View v1;
    private View v2;
    private View v3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager = findViewById(R.id.viewpager);
        v1 = getLayoutInflater().inflate(R.layout.layout_a,null);
        v2 = getLayoutInflater().inflate(R.layout.layout_b,null);
        v3 = getLayoutInflater().inflate(R.layout.layout_c,null);
        List<View> list = new ArrayList<>();
        list.add(v1);
        list.add(v2);
        list.add(v3);
        viewpager.setAdapter(new ViewAdapter(list));
    }
    class ViewAdapter extends PagerAdapter {

        List<View> list;

        public ViewAdapter(List<View> list) {
            this.list = list;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
//            return super.instantiateItem(container, position);
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//            super.destroyItem(container, position, object);
            container.removeView(list.get(position));
        }
    }
}